package cn.bugDance.domain.credit.service.redeem;

import cn.bugDance.domain.credit.model.entity.CreditShopCartEntity;
import cn.bugDance.domain.credit.model.entity.UserCreditAccountEntity;
import cn.bugDance.domain.credit.repository.ICreditRepository;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class CreditRedeemService implements ICreditRedeemService{

    @Resource
    private ICreditRepository creditRepository;


    @Override
    public void creditRedeem(CreditShopCartEntity creditShopCartEntity) {
        String userId = creditShopCartEntity.getUserId();
        Long sku = creditShopCartEntity.getSku();
        Integer creditRedeemAmount = creditShopCartEntity.getRedeemCreditAmount();

        if(StringUtils.isBlank(userId) || null == sku || null == creditRedeemAmount){
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        UserCreditAccountEntity userCreditAccountEntity = creditRepository.queryUserCreditAccount(userId);
        if(null == userCreditAccountEntity){
            log.warn("【CreditRedeemService ->】: 用户不存在积分账户, userId: {}", userId);
            throw new AppException(ResponseCode.NO_EXIST_CREDIT_ACCOUNT.getCode(), ResponseCode.NO_EXIST_CREDIT_ACCOUNT.getInfo());
        }


    }
}
